//
//  VideoViewController.m
//  BCMobileVideo
//
//  Created by David McGaffin on 1/26/10.
//  Copyright 2010 Brightcove, Inc.. All rights reserved.
//

#import "Constants.h"
#import "VideoViewController.h"
#import "BCMediaAPI.h"
#import "BCPlayer.h"

@implementation VideoViewController

@synthesize playlist;

- (void)viewDidLoad {
    [super viewDidLoad];

    player = [[BCPlayer alloc] init];
    player.delegate = self;
    player.showRelatedMoviesOnComplete = YES;
}


- (void)viewWillAppear:(BOOL)animated {

    self.title = playlist.name;
    [self.tableView reloadData];

    [super viewWillAppear:animated];
}


- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];

    self.playlist = nil;
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
	// Release any retained subviews of the main view.
    [player release];
    player = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [playlist.videos count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
    BCVideo *video = [playlist.videos objectAtIndex:indexPath.row];
	cell.textLabel.text = video.name;
    cell.detailTextLabel.text = video.shortDescription;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BCVideo *video = [playlist.videos objectAtIndex:indexPath.row];
    
    player.popularVideos = [self getPopularVideos:video.videoId];
    player.relatedVideos = [self getRelatedVideos:video.videoId];
    [player play:video];
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark BCPlayer delegate methods
- (NSArray *)getPopularVideos:(double)videoId {
    BCMediaAPI *api = [[BCMediaAPI alloc] initWithReadToken:kAPIKey];
    NSError *err;
    BCItemCollection *result = [[api findAllVideos:10 pageNumber:0 sortBy:BCSortByTypePlaysTrailingWeek 
                                         sortOrder:BCSortOrderTypeDESC getItemCount:NO 
                                         videoFields:nil customFields:nil error:&err] retain];
    
    NSArray *popularVideos = nil;
    
    if (!result) {
        NSString *errorString = [api getErrorsAsString:err];
        NSLog(@"Error: %s", errorString);
    } else {
        popularVideos = [[result items] retain];
    }        
    
    [api release];
    return popularVideos;
}

- (NSArray *)getRelatedVideos:(double)videoId {
    BCMediaAPI *api = [[BCMediaAPI alloc] initWithReadToken:kAPIKey];
    NSError *err;
    BCItemCollection *result = [[api findRelatedVideos:videoId error:&err] retain];
    NSArray *relatedVideos = nil;
    
    if (!result) {
        NSString *errorString = [api getErrorsAsString:err];
        NSLog(@"Error: %s", errorString);
    } else {
        relatedVideos = [[result items] retain];
    }        
    
    [api release];
    return relatedVideos;
}



@end

